/** localStorage, sessionStorage 二次封装-类 */
export class Storage2<T = string> {
  constructor(
    private storage: Storage = window.localStorage,
  ) { }

  get<D = T>(key: string) {
    let tmp = this.storage.getItem(key);
    if (typeof tmp === 'string') {
      try {
        tmp = JSON.parse(tmp);
      } catch { }
    }
    return tmp as D | null;
  }

  set(key: string, val: any) {
    if (val && typeof val === 'object') {
      val = JSON.stringify(val);
    }
    this.storage.setItem(key, val);
  }

  remove(key: string) {
    this.storage.removeItem(key);
  }
}

/** localStorage 二次封装 */
export const local = new Storage2();
/** sessionStorage 二次封装 */
export const session = new Storage2(sessionStorage);
